#!/bin/sh
#
### BEGIN INIT INFO
# Provides:          OpenXPKI 
# Required-Start:    
# Should-Start:      $time mysql
# Required-Stop:      
# Should-Stop:       mysql
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: OpenXPKI Trustcenter Software
### END INIT INFO
# 

# Check and Source sysconfig which also has the path override
# Check for existence of needed config file and read it
OPENXPKI_CONFIG=/etc/sysconfig/openxpki
test -r $OPENXPKI_CONFIG || { echo "$OPENXPKI_CONFIG not existing";
	if [ "$1" = "stop" ]; then exit 0;
	else exit 6; fi; }

# Read config	
. $OPENXPKI_CONFIG


# Check for missing binaries (stale symlinks should not happen)
# Note: Special treatment of stop for LSB conformance
OPENXPKI_BIN=`which openxpkictl`
test -x $OPENXPKI_BIN || { echo "$OPENXPKI_BIN not installed"; 
	if [ "$1" = "stop" ]; then exit 0;
	else exit 5; fi; }

. /etc/rc.status

# Reset status of this service
rc_reset

case "$1" in
    start)
	echo -n "Starting OpenXPKI "
        $OPENXPKI_BIN start

	# Remember status and be verbose
	rc_status -v
	;;
    stop)
	echo -n "Shutting down OpenXPKI "
        $OPENXPKI_BIN stop

	# Remember status and be verbose
	rc_status -v
	;;
    try-restart|condrestart)
	## Do a restart only if the service was active before.
	## Note: try-restart is now part of LSB (as of 1.9).
	## RH has a similar command named condrestart.
	if test "$1" = "condrestart"; then
		echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
	fi
	$0 status
	if test $? = 0; then
		$0 restart
	else
		rc_reset	# Not running is not a failure.
	fi
	# Remember status and be quiet
	rc_status
	;;
    restart)
	## Stop the service and regardless of whether it was
	## running or not, start it again.
	$0 stop
	$0 start

	# Remember status and be quiet
	rc_status
	;;
    force-reload|reload)

	echo -n "Reload service OpenXPKI "
        $OPENXPKI_BIN reload
	rc_status -v
        ;;
    status)
	echo -n "Checking for service OpenXPKI "
	$OPENXPKI_BIN status
        # NOTE: rc_status knows that we called this init script with
	# "status" option and adapts its messages accordingly.
	rc_status -v
	;;
    *)
	echo "Usage: $0 {start|stop|status|restart|reload}"
	exit 1
	;;
esac
rc_exit
